
package crm.salesOrderProcessing.managedbean;

import commonInfrastructure.accountManagement.entity.SalesmanEntity;
import commonInfrastructure.accountManagement.session.EmployeeSessionBean;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import crm.salesOrderProcessing.entity.SalesInquiryEntity;
import crm.salesOrderProcessing.session.SalesInquirySessionBean;
import java.sql.ResultSet;
import java.text.ParseException;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPdfExporter;


@ManagedBean
@ViewScoped
public class DeleteSalesInquiryManagedBean implements Serializable{
    @Resource(name = "merlionFoodERPDataSource")
    private DataSource merlionFoodERPDataSource;
  
    public Long id;  
    @EJB
    SalesInquirySessionBean salesInquirySessionBean;
    @EJB
    EmployeeSessionBean employee;
    private SalesInquiryEntity selectedSalesInquiry;
  

    public DeleteSalesInquiryManagedBean() {
        selectedSalesInquiry = new SalesInquiryEntity();
    }
    
    
    public void viewInquiry(ActionEvent event) throws IOException{
        SalesInquiryEntity si=(SalesInquiryEntity)event.getComponent().getAttributes().get("viewInquiry");
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("newInquiry", selectedSalesInquiry);
        FacesContext.getCurrentInstance().getExternalContext().redirect("viewSalesInquiry.xhtml");
    }
    

    public List<SalesInquiryEntity> getSalesInquiry() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        Long loginId = (Long) request.getSession().getAttribute("userId");
        SalesmanEntity user = employee.getSalesmanById(loginId);
        
        if (user.getSalesManLevel().equals("4")){
            return salesInquirySessionBean.getAllSalesInquiry();
        }
        else{
            return salesInquirySessionBean.getInquiryByUser(user);
        }
    }

    public void deleteSalesInquiry (ActionEvent event) throws ParseException{
       id=(Long)event.getComponent().getAttributes().get("deleteInquiry");
       salesInquirySessionBean.deleteSalesInquiry(id);
       selectedSalesInquiry = new SalesInquiryEntity();
    }

    public SalesInquiryEntity getSelectedSalesInquiry() {
        return selectedSalesInquiry;
    }

    public void setSelectedSalesInquiry(SalesInquiryEntity selectedSalesInquiry) {
        this.selectedSalesInquiry = selectedSalesInquiry;
    }
       
    public void exportToPDFBySQLConn(ActionEvent event){
        try {
            String strSQL = "SELECT I.ID, I.RAISEDATE,I.TOTALPRICE FROM SALESINQUIRYENTITY AS I";
            ResultSet rsEvents = merlionFoodERPDataSource.getConnection().prepareStatement(strSQL).executeQuery();
            JRResultSetDataSource jrResultSetDataSource = new JRResultSetDataSource(rsEvents);
           
            String path = FacesContext.getCurrentInstance().getExternalContext().getRealPath("jasperreports");
            
            HashMap parameters = new HashMap();
            parameters.put("IMAGEPATH", "http://localhost:8080/MerlionFoodERP-war/jasperreports/cherry.jpg");
            String fileName = path+"/testReport.jasper";
            String outFileName = path+"/testReport.pdf";
            System.err.println("********"+fileName);
            System.err.println("********"+outFileName);
            // Fill the report using an empty data source
            JasperPrint print = JasperFillManager.fillReport(fileName, parameters, jrResultSetDataSource);
            
            // Create a PDF exporter
            JRExporter exporter = new JRPdfExporter();
            
            // Configure the exporter (set output file name and print object)
            exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outFileName);
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
            
            // Export the PDF file
            exporter.exportReport();
        } 
        catch (JRException jrex){
            System.out.println("********** Jasperreport Exception");
            jrex.printStackTrace();
        }
        catch (Exception ex){
            ex.printStackTrace();
        }
    }
}
